﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Common;
using Phoenix.Common.Helper;
using Phoenix.Common.Util;
using Phoenix.Service.Model;
using Phoenix.Cache;

namespace Phoenix.AdminSite.Controllers
{
	public class ControllerBase : Controller
	{
        ICacheProvider _cacheProvider = CacheProviderFactory.HashTable().CreateCacheProvider();

        private string _Key = ConfigHelper.GetValue("SSOTicketKey");
        private string _IV = ConfigHelper.GetValue("SSOTicketIV");
        private string _DDomain = ConfigHelper.GetValue("SSODomain");
        private const string prefix = "Phoenix:AdminUser::";

		public ActionResult ShowMsg(string msgs, ActionStatus status, String url)
		{
			var retMsg = new
			{
				response = "",
				status = status.ToString().ToLower(),
				msg = msgs
			};
			return Json(retMsg); 
			
			

		}

        public bool CheckLogin()
        {
            AdminUserModel model = GetLoginInfo();

            if (model == null)
                return false;
            return true;
            
        }

        public AdminUserModel GetLoginInfo()
        {
            string token = Cookie.GetCookie("logon");
            if (!string.IsNullOrEmpty(token))
            {
                if (_cacheProvider.Contains(prefix+token))
                {
                    return _cacheProvider.Get(prefix + token) as AdminUserModel;
                }
                else
                {
                return null;
                }
               
            }
            else
            {
                return null;
            }
        }

       
        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="strSource">要解密的字符串</param>
        /// <returns>解密后的字符串</returns>
        public string Decrypt(string strSource)
        {
            Alogrim objAlogrim = new Alogrim(Alogrim.SymmProvEnum.DES);
            objAlogrim.Key = _Key;
            objAlogrim.IV = _IV;
            return objAlogrim.Decrypting(strSource);
        }

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public string Encrypt(string strSource)
        {

            Alogrim objAlogrim = new Alogrim(Alogrim.SymmProvEnum.DES);
            objAlogrim.Key = _Key;
            objAlogrim.IV = _IV;
            return objAlogrim.Encrypting(strSource);
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //string action = filterContext.Controller.ControllerContext.RouteData.Values["action"].ToString().ToLower();
            //string controller = filterContext.Controller.ControllerContext.RouteData.Values["controller"].ToString().ToLower();
            //if (action == "login")
            //{
               base.OnActionExecuted(filterContext);
            //}
            //else
            //{
            //    bool b = CheckLogin();
            //    if (b == false)
            //    {
            //        filterContext.Result = new RedirectResult("http://admin.86tc.com/home/login?done=" + filterContext.RequestContext.HttpContext.Server.UrlEncode("http://" + filterContext.RequestContext.HttpContext.Request.Url.Host + filterContext.RequestContext.HttpContext.Request.Url.PathAndQuery));

            //    }
            //}
           
        }

    }
}